import java.util.Map;

import IndexingLucene.*;

/**
 * !!! YOU CANNOT CHANGE ANYTHING IN THIS CLASS !!!
 * 
 * Main class for running your HW2.
 */
public class HW2MainLucene {

	public static void main(String[] args) throws Exception {
		
		// Main entrance
		HW2MainLucene hm2 = new HW2MainLucene();
		
		/* ----------------------------------------------------------------- */
		/* ---------- Write out & Read in index file for trecweb ----------- */
		/* ----------------------------------------------------------------- */
		long startTime =  System.currentTimeMillis();
		hm2.WriteIndex("trecweb");
		long endTime = System.currentTimeMillis();
		System.out.println("Index web corpus running time: " + (endTime - startTime) / 60000.0 + " min"); 
		
		startTime=System.currentTimeMillis();
		hm2.ReadIndex("trecweb", "acow");
		endTime=System.currentTimeMillis();
		System.out.println("Load index & retrieve running time: " + (endTime - startTime) / 60000.0 + " min");

		/* ----------------------------------------------------------------- */
		/* ---------- Write out & Read in index file for trectext ---------- */
		/* ----------------------------------------------------------------- */
		startTime = System.currentTimeMillis();
		hm2.WriteIndex("trectext");
		endTime = System.currentTimeMillis();
		System.out.println("Index text corpus running time: " + (endTime - startTime) / 60000.0 + " min");
		
		startTime = System.currentTimeMillis();
		hm2.ReadIndex("trectext", "yhoo");
		endTime = System.currentTimeMillis();
		System.out.println("Load index & retrieve running time: " + (endTime-startTime) / 60000.0 + " min");	
	}

	/**
	 * Build index file.
	 * @param dataType
	 * @throws Exception
	 */
	public void WriteIndex(String dataType) throws Exception {
		// Initiate preprocessed collection file reader
		PreProcessedCorpusReader corpus = new PreProcessedCorpusReader(dataType);
		
		// Initiate the output object
		MyIndexWriter ixWriter = new MyIndexWriter(dataType);
		
		// Initiate a doc object, which can hold document number and document content of a document
		Map<String, Object> doc = null;

		int count = 0;
		// Build index of corpus document by document
		while ((doc = corpus.nextDocument()) != null) {
			// Load document number and content of the document
			String docno = doc.keySet().iterator().next();
			char[] content = (char[]) doc.get(docno);
			
			// Index the current document
			ixWriter.index(docno, content); 
			
			count++;
			if(count%10000==0)
				System.out.println("Finish " + count + " docs.");
		}
		
		System.out.println("Totaly document count: " + count);
		
		// Close index writer 
		ixWriter.close();
	}
	
	/**
	 * Read index file.
	 * @param dataType
	 * @param token
	 * @throws Exception
	 */
	public void ReadIndex(String dataType, String token) throws Exception {
		// Initiate the index file reader
		MyIndexReader ixReader = new MyIndexReader(dataType);
		
		// Conduct retrieval
		int df = ixReader.DocFreq(token); //
		long cf = ixReader.CollectionFreq(token);
		System.out.println(" >> The token \"" + token + "\" appeared in " + df + " documents and " + cf + " times in total");
		if(df > 0){
			int[][] posting = ixReader.getPostingList(token);
			for(int ix=0; ix<posting.length; ix++){
				int docid = posting[ix][0];
				int freq = posting[ix][1];
				String docno = ixReader.getDocno(docid);
				System.out.printf("    %20s    %6d    %6d\n", docno, docid, freq);
			}
		}
		
		// Close index reader
		ixReader.close();
	}
}